var errorColor = "#FFFF00";
var minP = 5;
var space = '&nbsp;';
var required = 'Obligatoriu';
var minLength = 'Minim {0} caractere';
var minDigitLength = 'Minim {0} cifre';
var digitsOnly = 'Doar cifre';

function validateEmpty(field, errorField) {
	var value = field.value;
	document.getElementById(errorField).innerHTML = space;
	field.style.backgroundColor = 'white';

	if (value == null || value == "") {
		field.style.backgroundColor = errorColor;
		document.getElementById(errorField).innerHTML = required;
		return false;
	}
	return true;
}

function validateLength(field, errorField, size) {
	var value = field.value;
	document.getElementById(errorField).innerHTML = space;
	field.style.backgroundColor = 'white';

	if (value == null || value == "" || value.length < size) {
		field.style.backgroundColor = errorColor;
		document.getElementById(errorField).innerHTML = minLength.replace(
				'{0}', size);
		return false;
	}
	return true;
}

function validateTelephone(field, errorField) {
	var value = field.value;
	document.getElementById(errorField).innerHTML = space;
	field.style.backgroundColor = 'white';
	var numericExpression = /^[0-9]+$/;

	if (value == null || value == "" || value.length < 10) {
		field.style.backgroundColor = errorColor;
		document.getElementById(errorField).innerHTML = minDigitLength.replace(
				'{0}', 10);
		return false;
	} else {
		if (!value.match(numericExpression)) {
			field.style.backgroundColor = errorColor;
			document.getElementById(errorField).innerHTML = digitsOnly;
			return false;
		}
		return true;
	}
	return true;
}

function IsNumeric(evt, text)
//check for valid numeric strings
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	
	//is number or backspace
	if (charCode > 31 && (charCode >= 48 && charCode <= 57) 
			|| charCode == 8 || charCode == 37 
			|| charCode == 39 )
		return true;

	if (charCode == 110 && text.value.indexOf('.') == -1)
		return true;

	return false;
}

function validateComanda(f) {
	var form = document.getElementById(f);
	var fields = form.getElementsByTagName("input");
	var result = true;
	for ( var i = 0; i < fields.length; i++) {
		var name = fields[i].name;
		if (name == 'telefon') {
			result = validateTelephone(fields[i], name + 'Error') && result;
		} else {
			result = validateEmpty(fields[i], name + 'Error') && result;
		}
	}
	var textarea = form.getElementsByTagName('textarea')[0];
	result = validateEmpty(textarea, textarea.name + 'Error') && result;
	return result;
}
